﻿using HandyControl.Controls;
using HandyControl.Tools.Extension;
using KINLO.Data.Reporitory;
using KINLO.Model;
using KINLO.Model.InfoModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace KINLO.UI.Main.Services
{
    public static class GlobalValues
    {
        #region 字段
        static MachineReporitory DB = new MachineReporitory();
        static ConfigReporitory CfgDB = new ConfigReporitory();
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(K20021002CoreService));
        #endregion

        #region 方法

        static GlobalValues()
        {
            try
            {
                Init();
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0}:{1},{2}", "GlobalValues", "GlobalValues", ex));
               
            }
        }
        public static void Init()
        {
            var machines = DB.GetMachineByMachineType(0).ToList();
            //Application.Current.Dispatcher.Invoke(() =>
            //{
            //    System.Windows.MessageBox.Show("查询到的设备数量：" + machines.Count);
            //});

            FrontScanner = machines.Where(s => s.MachineType == 1).FirstOrDefault();

            BackScanner = machines.Where(s => s.MachineType == 3).FirstOrDefault();
            InjectionMachine = machines.Where(s => s.MachineType == 6).FirstOrDefault();

            FrontElecBalances = machines.Where(s => s.MachineType == 2).OrderBy(ss=>ss.ID).ToList();
            BackElecBalances = machines.Where(s => s.MachineType == 4).OrderBy(ss => ss.ID).ToList();
            RepairElecBalances = machines.Where(s => s.MachineType == 5).OrderBy(ss => ss.ID).ToList();
             
            var setttings = CfgDB.GetSettings();
            UITitle = setttings.UITitle;
            //InfoDataModel.Tmp.Load();
            Line = setttings.Line;
            Line = InfoDataModel.Tmp.BatchNo;
            SerialNum = setttings.SerialNum;
            LogSaveDays = setttings.LogSaveDays;
            RecordSaveDays = setttings.RecordSaveDays;
            UnQualifiedShowMsg = setttings.UnQualifiedShowMsg;
            TestElecBalance = setttings.TestElecBalance;
            PrintWeightLog = setttings.PrintWeightLog;
            NotFrontTrayAsPass = setttings.NotFrontTrayAsPass;
            WeightRef1 = setttings.WeightRef1;
            WeightWave1 = setttings.WeightWave1;
            WeightWave1Min = setttings.WeightWave1Min;
            WeightWave1Max = setttings.WeightWave1Max;
            WeightRef2 = setttings.WeightRef2;
            WeightWave2 = setttings.WeightWave2;
            WeightRef3 = setttings.WeightRef1;
            WeightWave3 = setttings.WeightWave1;
            FrontWeightPass = setttings.FrontWeightPass;
            WeightTimeout = setttings.WeightTimeout;
            FWeightEmptyLow = setttings.FWeightEmptyLow;
            InjValueEmptyLow = setttings.InjValueEmptyLow;
        }
        #endregion

        #region 属性

        /// <summary>
        /// 称重超时时间
        /// </summary>
        public static int? WeightTimeout { get; set; }
        /// <summary>
        /// 称重低下限判断为空
        /// </summary>
        public static decimal? FWeightEmptyLow { get; set; }
        /// <summary>
        /// 注液量低下限判断为空
        /// </summary>
        public static decimal? InjValueEmptyLow { get; set; }
        /// <summary>
        /// 前扫描器
        /// </summary>
        public static Machine FrontScanner { get; set; }
        /// <summary>
        /// 后扫描器
        /// </summary>
        public static Machine BackScanner { get; set; }
        /// <summary>
        /// 注液机
        /// </summary>
        public static Machine InjectionMachine { get; set; }
        /// <summary>
        /// 注液前电子称
        /// </summary>
        public static List<Machine> FrontElecBalances { get; set; }
        /// <summary>
        /// 注液后电子称
        /// </summary>
        public static List<Machine> BackElecBalances { get; set; }
        /// <summary>
        /// 补液电子称
        /// </summary>
        public static List<Machine> RepairElecBalances { get; set; }
        
        /// <summary>
        /// UI标题
        /// </summary>
        public static String UITitle { get; set; }
        /// <summary>
        /// 流水线号
        /// </summary>
        public static String Line { get; set; }
        /// <summary>
        /// 工件
        /// </summary>
        public static int? SerialNum { get; set; }
        /// <summary>
        /// 记录保存天数
        /// </summary>
        public static int? RecordSaveDays { get; set; }
        /// <summary>
        /// 日志保存天数
        /// </summary>
        public static int? LogSaveDays { get; set; }
        
        /// <summary>
        /// 不合格弹消息框
        /// </summary>
        public static bool? UnQualifiedShowMsg { get; set; }
        /// <summary>
        /// 电子称测试
        /// </summary>
        public static bool? TestElecBalance { get; set; }
        /// <summary>
        /// 打印称重记录
        /// </summary>
        public static bool? PrintWeightLog { get; set; }
        /// <summary>
        /// 没有前托盘视为合格
        /// </summary>
        public static bool? NotFrontTrayAsPass { get; set; }
        /// <summary>
        /// 注液量重参考值
        /// </summary>
        public static decimal? WeightRef1 { get; set; }
        /// <summary>
        /// 注液量重波动值
        /// </summary>
        public static decimal? WeightWave1 { get; set; }
        /// <summary>
        /// 注液量最小值
        /// </summary>
        public static decimal? WeightWave1Min { get; set; }
        /// <summary>
        /// 注液量最大值
        /// </summary>
        public static decimal? WeightWave1Max { get; set; }
        /// <summary>
        /// 前称重参考值
        /// </summary>
        public static decimal? WeightRef2 { get; set; }
        /// <summary>
        /// 前称重波动值
        /// </summary>
        public static decimal? WeightWave2 { get; set; }
        /// <summary>
        /// 后重参考值
        /// </summary>
        public static decimal? WeightRef3 { get; set; }
        /// <summary>
        /// 后重参考波动值
        /// </summary>
        public static decimal? WeightWave3 { get; set; }
        /// <summary>
        /// 启用前称重检查
        /// </summary>
        public static bool? EnableFrontInjectCheck { get; set; }
        /// <summary>
        /// 启用后称重检查
        /// </summary>
        public static bool? EnableBackInjectCheck { get; set; }
        /// <summary>
        /// 最小值
        /// </summary>
        public static decimal? MinValue { get; set; }


        /// <summary>
        /// 清除清称重数据密码
        /// </summary>
        public static string FrontWeightPass { get; set; }

        #endregion

    }
}
